import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { IMapUIModule } from "../../../modules/interfaces/IMapUIModule";
import { AStarPoint } from "../scene/map/AStarPoint";
import { Game } from "../Game";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SEntityInfo } from "../../../Message/BroadCast/SEntityInfo";
import { SEntityMoveInfo } from "../../../Message/BroadCast/SEntityMoveInfo";
import { SMapAllEntityPoint } from "../../../Message/BroadCast/SMapAllEntityPoint";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { SBossPoints } from "../../../Message/Public/SBossPoints";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { SBossRefreshInfo } from "../../../Message/Public/SBossRefreshInfo";
import { SGuildPartyOpen } from "../../../Message/Game/SGuildPartyOpen";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { WindowEvent } from "../../component/window/WindowEvent";
import { IView } from "../../mvc/interfaces/IView";
import { MapUIModule } from "../../../modules/MapUIModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ECmdBroadCast } from "../../../Message/Command/ECmdBroadCast";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { ServerCommand } from "../mvc/ServerCommand";
//class MapUIController
    
    export  class MapUIController extends Controller
    {
       

        protected onSceneUpdate(arg1: DataEvent): void
        {
            cache.mapui.onSceneChangeAfter();
            if (cache.mapui.bossDeathList) 
            {
                cache.scene.updateBossRefreshPoint(cache.mapui.bossDeathList.bossIds);
            }
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).updateMap(cache.mapui.currentMapId);
                (_view as IMapUIModule).resetHangText();
            }
            return;
        }

        /* internal  */onMapFindPath(arg1: DataEvent): void
        {
            cache.mapui.mapFindPath = arg1.data as Array<any>;
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).updatePath(cache.mapui.mapFindPath);
            }
            return;
        }

        /* internal  */onMapUpdateRolePosition(arg1: DataEvent): void
        {
            cache.mapui.currentPosition = arg1.data as AStarPoint;
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).updatePostion(Game.scene.rolePlayer.currentPoint);
            }
            return;
        }

        /* internal  */onChangePosition(arg1: DataEvent): void
        {
            cache.mapui.currentPosition = arg1.data as AStarPoint;
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).changePosition(cache.mapui.currentPosition);
            }
            return;
        }

        /* internal  */onRoleMoveEnd(arg1: PlayerEvent): void
        {
            cache.mapui.currentPosition = RolePlayer.instance.currentPoint;
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).onMoveEnd(cache.mapui.currentPosition);
            }
            return;
        }

        /* internal  */onConveyRes(arg1: DataEvent): void
        {
            cache.mapui.currentPosition = RolePlayer.instance.currentPoint;
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).onConveyEnd(cache.mapui.currentPosition);
            }
            return;
        }

        /* internal  */onSceneChange(arg1: DataEvent): void
        {
            cache.mapui.onSceneChangeBefore();
            if (_view) 
            {
                (_view as IMapUIModule).sceneChangeBefore();
            }
            return;
        }

        /* internal  */onEntityEnter(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SEntityInfo;
            cache.mapui.entityEnter(loc1);
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).entityEnter(loc1);
            }
            return;
        }

        /* internal  */onEntityUpdate(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SEntityMoveInfo;
            var loc2=cache.mapui.entityUpdate(loc1);
            if (!(loc2 == null) && _view && !_view.isHide) 
            {
                (_view as IMapUIModule).entityUpdate(loc2);
            }
            return;
        }

        /* internal  */onMapAllEntityUpdate(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SMapAllEntityPoint;
            if (loc1 && _view && !_view.isHide) 
            {
                (_view as IMapUIModule).allEntityPointUpdate(loc1);
            }
            return;
        }

        /* internal  */onEntityLeft(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SEntityId;
            cache.mapui.entityLeft(loc1);
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).entityLeft(loc1);
            }
            return;
        }

        /* internal  */onMapBossEntityPointRes(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SBossPoints;
            if (loc1.mapId == MapFileUtil.mapID) 
            {
                cache.mapui.bossPoints = loc1;
                if (_view && !_view.isHide) 
                {
                    (_view as IMapUIModule).updateBossPoint(loc1);
                }
            }
            return;
        }

        /* internal  */onMapBossPointRes(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SBossRefreshInfo;
            cache.mapui.bossDeathList = loc1;
            var loc2=cache.scene.updateBossRefreshPoint(loc1.bossIds);
            if (loc2) 
            {
                if (_view && !_view.isHide) 
                {
                    (_view as IMapUIModule).updateBossRefreshPoint(Game.sceneInfo.bossRefreshs);
                }
            }
            return;
        }

        public updatePassPoint(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).updatePassPoint();
            }
            return;
        }

        /* internal  */onGuildPartyOpenHandRes(arg1: SGuildPartyOpen): void
        {
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).onGuildPartyOpenHandRes();
            }
            return;
        }

        /* internal  */onShowCityMapHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            if (loc1 != 0) 
            {
                if (view.isHide) 
                {
                    view.show();
                }
                (_view as IMapUIModule).updataCityMap(loc1);
            }
            return;
        }

        /* internal  */onHuntLocationHandler(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).targetLocation(cache.huntTask.huntTaskInfo.mapId, cache.huntTask.huntTaskInfo.px, cache.huntTask.huntTaskInfo.py);
            }
            return;
        }

        /* internal  */onHuntLocationHideHandler(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).hideLocation();
            }
            return;
        }

        /* internal  */onNPCShowOrHideHandler(arg1: DataEvent): void
        {
            if (_view && !_view.isHide) 
            {
                (_view as IMapUIModule).updateNPCShow(arg1.data.npcId, arg1.data.show);
            }
            return;
        }

        /* internal  */onCrossMainCityBelongInfoChangeHandler(arg1): void
        {
            if (GameMapUtil.curMapState.isCrossMainCity) 
            {
                if (_view && !_view.isHide) 
                {
                    (_view as IMapUIModule).updateMapName();
                }
            }
            return;
        }

        /* internal  */onWindowShowHandler(arg1: WindowEvent): void
        {
            (_view as IMapUIModule).updateMap(cache.mapui.currentMapId);
            (_view as IMapUIModule).updatePath(cache.mapui.mapFindPath);
            (_view as IMapUIModule).updatePostion(Game.scene.rolePlayer.currentPoint);
            (_view as IMapUIModule).updatePassPoint();
            (_view as IMapUIModule).updateBossRefreshPoint(Game.sceneInfo.bossRefreshs);
            if (cache.mapui.bossPoints && cache.mapui.bossPoints.mapId == MapFileUtil.mapID) 
            {
                (_view as IMapUIModule).updateBossPoint(cache.mapui.bossPoints);
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new MapUIModule();
            loc1.addEventListener(WindowEvent.SHOW, this.onWindowShowHandler);
            return loc1;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.MapUpdateRolePosition, this.onMapUpdateRolePosition);
            RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onRoleMoveEnd);
            Dispatcher.addEventListener(EventName.MapFindPath, this.onMapFindPath);
            Dispatcher.addEventListener(EventName.Convey_Res, this.onConveyRes);
            Dispatcher.addEventListener(EventName.ChangeScene, this.onSceneChange);
            Dispatcher.addEventListener(EventName.ChangePosition, this.onChangePosition);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapEntity, this.onEntityEnter);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapEntityPoint, this.onEntityUpdate);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapAllEntityPoint, this.onMapAllEntityUpdate);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapEntityLeft, this.onEntityLeft);
            NetDispatcher.addCmdListener(ECmdBroadCast._ECmdBroadcastMapBossEntityPoint, this.onMapBossEntityPointRes);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDeathBossList, this.onMapBossPointRes);
            NetDispatcher.addCmdListener(ServerCommand.GuildPartyOpenCommand, this.onGuildPartyOpenHandRes);
            Dispatcher.addEventListener(EventName.MapUIShowCityMap, this.onShowCityMapHandler);
            Dispatcher.addEventListener(EventName.HuntLocationReq, this.onHuntLocationHandler);
            Dispatcher.addEventListener(EventName.HuntLocationHide, this.onHuntLocationHideHandler);
            Dispatcher.addEventListener(EventName.MapUINPCShowOrHide, this.onNPCShowOrHideHandler);
            Dispatcher.addEventListener(EventName.MapUIUpdatePassPoint, this.updatePassPoint);
            NetDispatcher.addCmdListener(ServerCommand.CrossMainCity_BelongInfoChange, this.onCrossMainCityBelongInfoChangeHandler);
            return;
        }
    }
